using Hangfire;
using Hangfire.Dashboard.BasicAuthorization;
using QuickCode.Application;
using QuickCode.IApplication;
using QuickCode.Job.HangFire;
using static QuickCode.Job.HangFire.DyDashboardAuthorizationFilter;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


#region HangFile注入
//StorageType.UseStorage(builder);
var connectstring = builder.Configuration["Environment:DBSetting:ConnectStr"];
builder.Services.UseStorage(connectstring);
//定时业务注入
builder.Services.AddSingleton<IJobApplication, JobApplication>();

builder.Services.AddHangfireServer();
#endregion
//日志
builder.Host.ConfigureLogging(logging =>
{
    logging.ClearProviders();
    logging.AddConsole();
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

//配置hangfire访问路径
app.UseHangfireServer();
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
    Authorization = new[] { new MyDashboardAuthorizationFilter() }
});

//定时业务操作
var JobService = app.Services.GetRequiredService<IJobApplication>();
RecurringJob.AddOrUpdate(() => Console.WriteLine("Recurring!"), Cron.Minutely());
RecurringJob.AddOrUpdate("WriteLog", () => JobService.WriteLog(), Cron.Minutely());

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
